/*
 *         Copyright (c) KUBOTA Corporation and HUDSON SOFT,
 *                  1994, 1995 All Rights Reserved
 *
 *  This software comprises unpublished confidential information of
 *  KUBOTA Corp. and HUDSON SOFT. It may not be used, copied or made 
 *  available to anyone, except in accordance with the license under
 *  which it is furnished.
 */
static char SccsId[] = "@(#)run.c	1.6	9/19/95";
#include <farl.h>
#include <agl.h>
#define SIDE_OBJ_SIZE 22
#define X_START 0x280
#define Y_START 0x0
#define Z_START 0x280
#define MOVE_STEP 0x8
#define ROT_STEP 20

int cube[] = {
#include "cubestp.h"
};

void
main()
{
     register int	i;
     register fix187	xoff, yoff, zoff; /* 現在のキューブの位置 */
     register int	roty;
     object	oa[SIDE_OBJ_SIZE], omove;
     Material	mt;
     fix187	oa_off[SIDE_OBJ_SIZE][3] = {
     {0x280, 0x0, 0x200}, {0x300, 0x0, 0x200}, {0x380, 0x0, 0x200},   
     {0x400, 0x0, 0x200}, {0x480, 0x0, 0x200},   
     {0x480, 0x0, 0x180}, {0x480, 0x0, 0x100}, {0x480, 0x0, 0x80},
     {0x480, 0x0, 0x0},		/* ここまで、カーブ内側のキューブ位置 */
     {0x280, 0x0, 0x300}, {0x300, 0x0, 0x300}, {0x380, 0x0, 0x300},   
     {0x400, 0x0, 0x300}, {0x480, 0x0, 0x300}, {0x500, 0x0, 0x300},
     {0x580, 0x0, 0x300}, {0x580, 0x0, 0x280}, {0x580, 0x0, 0x200},
     {0x580, 0x0, 0x180}, {0x580, 0x0, 0x100}, {0x580, 0x0, 0x80},
     {0x580, 0x0, 0x0}};	/* ここまで、カーブ外側のキューブ位置 */

     FX73init();
     FX73_cmap_i6c3();
     AGLInit();

     FarlSetPEICMaskReg(FARL_ICMASK_I6C3);
     FarlSetTEControlReg(FARL_TE_LTEN|FARL_TE_ICM| FARL_TE_SPEN| FARL_TE_BCE);

#if 1
     /* Hidden Clear */
     FarlSetMemReg(FARL_DISPLAY_CONTROL_REG,
		   FARL_DISPLAY_CONTROL_BCM0 | FARL_DISPLAY_CONTROL_HCE);
     FarlSetPEMaskReg(0x3fff);
     FarlSetPEClearMaskReg(0x3fff);
     FarlSetPEClearColorReg(0x807);
     /**/
#endif
     FarlValidateSwap();

     for (i = 0; i < SIDE_OBJ_SIZE; i++) {
	  oa[i].geometryTop = cube;
	  oa[i].geometryLength = sizeof(cube) / sizeof(int);
	  oa[i].material = NULL;
     }

     mt.ambient = 0x0800;
     mt.diffuse = 0x2000;
     mt.specular = 0x4000;
     oa[0].material = &mt;
     AGLSetMaterial(&mt, 1);

     omove.geometryTop = cube;
     omove.geometryLength = sizeof(cube) / sizeof(int);
     omove.material = &mt;

     AGLResetLight(AGL_LIGHT0);
     AGLRotateLightX(AGL_LIGHT0, 3150);

     xoff = X_START; yoff = Y_START; zoff = Z_START;
     roty = 0;
     while (1) {
	  for (i = 0; i < SIDE_OBJ_SIZE; i++) {
	       AGLResetObj(&oa[i]);
	       AGLScaleObj(&oa[i], 0x100, 0x100, 0x100);
	       AGLTranslateObj(&oa[i],
			       oa_off[i][0], oa_off[i][1], oa_off[i][2]);
	  }

	  AGLResetObj(&omove);
	  AGLScaleObj(&oa[i], 0x100, 0x100, 0x100);
	  AGLRotateObjY(&omove, roty);
	  AGLTranslateObj(&omove, xoff, yoff, zoff);
	  if (xoff <  0x500) xoff += MOVE_STEP;
	  else if (zoff != Z_START) {
	       zoff -= MOVE_STEP;
	       if (zoff <= 0x0) {
		    xoff = X_START;
		    zoff = Z_START;
		    roty = 0;
	       }
	  } else {
	       if (roty < 900) roty += ROT_STEP;
	       else {
		    roty = 900;
		    zoff -= MOVE_STEP;
	       }
	  }

	  AGLResetCamera();
	  AGLTranslateCamera(0x580, 0x100, 0x300);
	  AGLLookAtCamera(xoff, yoff, zoff, 0x0, 0x7fff, 0x0);
	  AGLPerspectiveCamera(0xff88);

	  for (i = 0; i < SIDE_OBJ_SIZE; i++) AGLFixPutObj(&oa[i]);
	  AGLFixPutObj(&omove);

	  AGLShot();
     }
     return;
}
